﻿package  {
	
	import flash.events.Event;
	import flash.system.Security;
	import flash.utils.setTimeout;
	
	import game.Game;
	import game.lang.Lang;
	import game.context.Context;
	import game.context.ContextObject;
	import game.socket.Sockets;
	import game.network.*;
	import game.libs.Libs;
	import game.objects.LibObject;
	import game.objects.CompileObject;
	import game.Config;
	import game.music.Music;
	import game.objects.DynamicObject;
	import game.objects.DynamicElement;
	import game.objects.FiltersObject;
	import game.ui.Text;
	
	public class nfs extends Game {
		
		public function nfs():void {
			this.addEventListener(Event.ADDED_TO_STAGE, added);
		}
		
		private function added(event:Event):void {
			//инициализация
			config	= new Config;
			lang	= new Lang (config.compile.lang);
			libs	= new Libs;
			socket	= new Sockets;
			network = new Network;
			music	= new Music;
			
			//создаем скины
			setupInterface();
			
			if (config.compile.player==CompileObject.External || config.compile.player==CompileObject.ActiveX || config.compile.player==CompileObject.PlugIn) {
				//разрешаем доступ
				Security.allowDomain("*");
			
				//контекст
				this.contextMenu = Context.get([
					new ContextObject('Русский', function() { lang.local = Lang.RU; }),
					new ContextObject('English', function() { lang.local = Lang.EN; })
				]);
			}
			
			//применяем stage из конфига
			for (var key_stage:String in config.stage)
				stage[key_stage] = config.stage[key_stage];
			
			//добавляем библиотеки
			for (var key_lib:String in config.libs)
				libs.add(config.libs[key_lib]);
			
			addChild(network);
			
			initNetwork();
			network.addEventListener ( NetworkEvent.COMPLETE,	function (event:NetworkEvent) { init(); } );
			network.addEventListener ( NetworkEvent.GET_TOKEN,	updateToken );
			network.addEventListener ( NetworkEvent.ERROR,		function (event:NetworkEvent) { error(event.data.type, event.data.id, event.data.text, event.data.params); } );
			network.init ( stage.loaderInfo as Object );
			
			//lang.set(test, 'IDS_TEST');
			
		}
		
		private function setupInterface():void {
			//Ошибка
			config.skins.push( new DynamicObject( DynamicObject.ERROR, 
				new ErrorCenter,
				null, 
				null, 
				new ErrorLeft, 
				DynamicObject.reverse(new ErrorLeft, 'x'), 
				'350px', 
				null, 
				DynamicObject.POSITION_CENTER, 
				DynamicObject.POSITION_CENTER, 
				'6px', 
				'-15px', 
				'-17px', 
				'17px', 
				false,
				new <DynamicElement>[
					new DynamicElement ( DynamicElement.TYPE_BUTTON, 'game.buttons.CloseButton',	'btnClose',	{x: '-18px',	y: '0px'} ),
					new DynamicElement ( DynamicElement.TYPE_TEXT, DynamicElement.CLASS_TEXT, 		'header',	{x: '0px',		y: '29px',	width: '100%', filters: FiltersObject.SHADOW_WHITE, align: Text.CENTER} ),
					new DynamicElement ( DynamicElement.TYPE_TEXT, DynamicElement.CLASS_TEXT, 		'text',		{x: '0px',		y: '70px',	width: '100%', filters: FiltersObject.SHADOW_WHITE, align: Text.CENTER} ),
					new DynamicElement ( DynamicElement.TYPE_TEXT, DynamicElement.CLASS_TEXT, 		'sub',		{x: '10px',		y: '120px',	width: '100%', filters: FiltersObject.SHADOW_WHITE, align: Text.CENTER} )
				]
			));
			//Друзья
			
		}
		
		private function updateToken ( event:NetworkEvent ):void {
			if (config.me.expires_in>0 && config.me.expires_in<Config.EXPIRES_MAX) {
				//есть ли время жизни сессии, и если не больше чем в конфиге
				setTimeout(network.init, config.me.expires_in*1000, stage.loaderInfo as Object);
			}
		}
		
	}
	
}
